package sdk.webview.fmc.com.fmcsdk.util;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import sdk.webview.fmc.com.fmcsdk.R;

/**
 * Author: yangdm
 * Description:(公共标题栏)
 */
public class TitleBar extends FrameLayout implements View.OnClickListener {
    private int[] colors;
    ImageView ivBack;
    TextView tvTitle;
    TextView tvRight;
    ImageView ivRight;
    RelativeLayout rlTitlebar;
    ImageView ivRightTwo;

    private Context context;

    public TitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.view_title, this);
         ivBack = (ImageView) findViewById(R.id.iv_back);
        ivRight = (ImageView) findViewById(R.id.iv_right);
        ivRightTwo = (ImageView) findViewById(R.id.iv_right_two);
        tvRight = (TextView) findViewById(R.id.tv_right);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        rlTitlebar = (RelativeLayout) findViewById(R.id.rl_titlebar);
        showBack();

    }

    /**
     * 设置标题
     */
    public void setTitle(String title) {
        tvTitle.setVisibility(View.VISIBLE);
        tvTitle.setText(title);

    }


    /**
     * 显示返回键
     */
    public void showBack() {
        ivBack.setVisibility(View.VISIBLE);
        ivBack.setOnClickListener(this);
    }


    /**
     * 隐藏返回键
     */
    public void hideBack() {
        ivBack.setVisibility(View.GONE);
    }


    /**
     * 默认关闭当前Activity
     */
    @Override
    public void onClick(View v) {
        ((Activity) getContext()).finish();
    }


    /**
     * 设置标题栏最右方文字(自定义颜色)
     */
    public void setRightContent(String rightContent, int purple) {
        tvRight.setVisibility(View.VISIBLE);
        tvRight.setTextColor(purple);
        tvRight.setText(rightContent);
    }

    /**
     * 设置标题栏最右方文字
     */
    public void setRightContent(String rightContent) {
        tvRight.setVisibility(View.VISIBLE);
        tvRight.setText(rightContent);
    }

    /**
     * 获取右边文字
     *
     * @return
     */
    public String getRightContent() {
        String s = tvRight.getText().toString();
        return s;
    }

    /**
     * 右边点击事件
     *
     * @param listener
     */
    public void setRightOnClick(OnClickListener listener) {
        tvRight.setOnClickListener(listener);
    }


    /**
     * 设置右边的图片
     */
    public void setRightImage() {
        ivRight.setVisibility(View.VISIBLE);
    }

    public void setRightImage(int img) {
        ivRight.setVisibility(View.VISIBLE);
        ivRight.setImageResource(img);
    }

    /**
     * 右边图片点击事件
     *
     * @param listener
     */
    public void setRightImgOnClick(OnClickListener listener) {
        ivRight.setOnClickListener(listener);
    }



    /**
     * 设置右边的第二个收藏图片
     */
    public void setRightImageTwo() {
        ivRightTwo.setVisibility(View.VISIBLE);
    }

    public void setRightImageTwo(int img) {
        ivRightTwo.setVisibility(View.VISIBLE);
        ivRightTwo.setImageResource(img);
    }

    /**
     * 右边第二个图片点击事件
     *
     * @param listener
     */
    public void setRightImgOnClickTwo(OnClickListener listener) {
        ivRightTwo.setOnClickListener(listener);
    }
}
